package com.jy.it.ui;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {
	@RequestMapping(value="/upload",method=RequestMethod.GET)
	public String upload() {
		return "a";
	}
	
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public String upload(String title,MultipartFile photoFile,HttpServletRequest req) throws IllegalStateException, IOException {
		if(!photoFile.isEmpty()) {
		String prjdir=req.getServletContext().getRealPath("/");
		String webapps=new File(prjdir).getParent();
		String ext=photoFile.getOriginalFilename().substring(photoFile.getOriginalFilename().lastIndexOf("."));
		String imgname=UUID.randomUUID().toString()+ext;
		if(new File(webapps+"/imgs/").exists()==false) 
			new File(webapps+"/imgs/").mkdirs();
		
		photoFile.transferTo(new File(webapps+"/imgs/"+imgname));
	}
		
		System.out.println(title);
		System.out.println(photoFile.getOriginalFilename());
		
		return "a";
	}
}
